﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Linq.Expressions;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint.WebControls;
using ZhukBlogLinqExamples.FieldTypes.CustomUser.Controls;
using ZhukBlogLinqExamples.Model;
using ZhukBlogLinqExamples.Repositories;

namespace ZhukBlogLinqExamples.CONTROLTEMPLATES
{
    public partial class EmployeeFinder : UserControl
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            var depRepository = new DepartmentRepository(true);
            var branchRepository = new BranchRepository(true);
            var companyRepository = new CompanyRepository(true);
            var allDeps = depRepository.GetEntityCollection()
                .OrderBy(x => x.Title);
            var allBranches = branchRepository.GetEntityCollection()
                .OrderBy(x => x.Title);
            var allCompanies = companyRepository.GetEntityCollection()
                .OrderBy(x => x.Title);
            foreach (var company in allCompanies)
            {
                var nodeCompany = new TreeNode(company.Title, company.Id.ToString());
                nodeCompany.ImageUrl = "/_layouts/images/urn-content-classes-workspace16.gif";
                nodeCompany.SelectAction = TreeNodeSelectAction.Expand;
                foreach (var branch in allBranches.Where(x => x.CompanyInId == company.Id))
                {
                    var nodeBranch = new TreeNode(branch.Title, branch.Id.ToString());
                    nodeBranch.ImageUrl = "/_layouts/images/MSMEML.gif";
                    nodeBranch.SelectAction = TreeNodeSelectAction.Expand;
                    foreach (var department in allDeps.Where(x => x.BranchId == branch.Id))
                    {
                        var nodeDepartment = new TreeNode(department.Title, department.Id.ToString());
                        nodeDepartment.ImageUrl = "/_layouts/images/MTGICON.gif";
                        nodeBranch.ChildNodes.Add(nodeDepartment);
                    }
                    nodeCompany.ChildNodes.Add(nodeBranch);
                }
                CompanyTree.Nodes.Add(nodeCompany);
            }
        }

        public PickerDialog Dialog { get; set; }

        protected void CompanyTree_SelectedNodeChanged(object sender, EventArgs e)
        {
            var node = CompanyTree.SelectedNode;
            var employeeRepository = new EmployeeRepository(true);
            if (node.Parent.Parent is TreeNode)
            {
                var depId = Convert.ToInt32(node.Value);
                var employees = employeeRepository.GetEntityCollection(x => x.DepartmentId == depId);
                var table = CustomUserFieldSearch.CreateDataTable();
                foreach (var employee in employees)
                {
                    var row = table.NewRow();
                    row["Id"] = employee.Id;
                    row["Title"] = employee.Title;
                    row["DepartmentTitle"] = employee.DepartmentTitle;
                    row["CellPhone"] = employee.CellPhone;
                    table.Rows.Add(row);
                }
                Dialog.Results = table;
                Dialog.ResultControl.PageSize = table.Rows.Count;
                var rc = Dialog.ResultControl as TableResultControl;
                if (rc != null)
                {
                    rc.ColumnNames.AddRange(new[] { "Id", "Title", "DepartmentTitle", "CellPhone" });
                    rc.ColumnDisplayNames.AddRange(new[] { "ИД", "ФИО", "Отдел", "Телефон" });
                    rc.ColumnWidths.AddRange(new[] { "15%", "35%", "25%", "25%" });
                }
            }
        }
    }
}
